<template>
	<view class="content">
		<view class="form-block" style="border-radius:30rpx 30rpx 0 0 ;">
			<view class="form-title">编辑教育经历</view>
			<view class="form-item">
				<view class="form-label">学校名称</view>
				<view class="form-content">
					<Combox labelKey="schoolName" valueKey="schoolName" :candidates="schoolList" :border="false"
						:value="form.schoolName" emptyTips="无匹配项" placeholder="请填写学校名称" class="form-ctrl"
						@input="schoolNameChange"></Combox>
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view class="form-item">
				<view class="form-label">学历</view>
				<view class="form-content">
					<picker class="form-ctrl" :range="education" range-key="data" :value="educationIndex"
						@change="bindEducation">{{education[educationIndex]['data']}}</picker>
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view class="form-item">
				<view class="form-label">专业名称</view>
				<view class="form-content">
					<Combox labelKey="fieldName" valueKey="fieldName" :candidates="field" :border="false"
						emptyTips="无匹配项" placeholder="请选择专业" :value="form.major" @input="fieldNameChange"
						class="form-ctrl"></Combox>
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view class="form-item">
				<view class="form-label">时间</view>
				<view class="form-content">
					<picker mode="date" class="form-ctrl" fields="day" :value="form.startTime" @change="bindStart">
						<view class="uni-input">{{form.startTime}}</view>
					</picker>
					<picker mode="date" class="form-ctrl" fields="day" :value="form.endTime" @change="bindEnd">
						<view class="uni-input">{{form.endTime}}</view>
					</picker>
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view class="comment-block">
				<view class="label">在校经历(选填)</view>
				<textarea v-mode="form.experience" placeholder="1.在校担任职务...2.获得荣誉3.所学主要课程" class="textarea" />
			</view>
			<view class="btn" @click="handleSave">保存</view>
		</view>
	</view>
</template>

<script>
	import {
		submitEducation,
		getCusEducation,
		getFieldApi,
		getSchoolApi
	} from '@/api/resume.js';
	import {
		getEducation as getEducationApi
	} from '@/api/config.js'
	import {
		validate
	} from '@/util/validate';
	import {
		getCurrentTime,
		getPickerIndex,
		throttle
	} from '@/util/util';
	import Combox from '@/page_other/components/Combox/Combox'
	export default {
		components: {
			Combox
		},
		data() {
			return {
				end: getCurrentTime('d'),
				form: {
					schoolName: '',
					education: null,
					major: '',
					startTime: getCurrentTime('d'),
					endTime: getCurrentTime('d'),
					id: 0,
					experience: ''
				},
				field: [],
				schoolList: [],
				rules: {
					schoolName: {
						name: '学校名称',
						required: true
					},
					education: {
						name: '学历',
						required: true
					},
					major: {
						name: '专业名称',
						required: true
					},
					startTime: {
						name: '开始时间',
						required: true
					},
					endTime: {
						name: '结束时间',
						required: true
					}
				},
				education: [],
				educationIndex: 0
			}
		},
		methods: {
			schoolNameChange(e) {
				this.form.schoolName=e;
				this.getSchool()
			},
			fieldNameChange(e) {
				this.form.major=e;
				this.getField()
			},
			bindStart(e) {
				this.form.startTime = e.detail.value
			},
			bindEnd(e) {
				this.form.endTime = e.detail.value
			},
			bindEducation(e) {
				this.educationIndex = e.detail.value
			},
			handleSave() {
				let form = JSON.parse(JSON.stringify(this.form));
				form.education = this.education[this.educationIndex].id
				if (validate(form, this.rules)) {
					uni.showLoading({
						title: '提交中',
						mask: true
					})
					submitEducation(form).then(() => {
						uni.$emit('refresh', 4)
						this.showToast('保存成功', () => {
							uni.navigateBack()
						})
					})
				}
			},
			getDetail(id) {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getCusEducation({
					id
				}).then((res) => {
					this.educationIndex = getPickerIndex(res.education, this.education)
					this.form = res
				})
			},
			getEducation(id) {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getEducationApi({}).then((res) => {
					this.education = res
					if (id) {
						this.getDetail(id)
					}
				})
			},
			getField() {
				getFieldApi({keyword:this.form.major}).then((res) => {
					this.field = res??[]
				})
			},
			getSchool() {
				getSchoolApi({keyword:this.form.schoolName}).then((res) => {
					this.schoolList = res??[]
				})
			}
		},
		onLoad(e) {
			this.getEducation(e.id);
		},

	}
</script>

<style lang="scss">
	page {
		background: #fff;
	}

	.content {
		width: 100%;

		.btn {
			width: 350rpx;
			height: 80rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background: #FF7626;
			color: #fff;
			font-size: 28rpx;
			border-radius: 40rpx;
			margin: 0 auto;
			margin-top: 30rpx;
		}

		.user-photo-block {
			width: 100%;
			height: 450rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			.user-photo {
				width: 180rpx;
				height: 180rpx;
				border-radius: 50%;
			}
		}

		.form-block {
			width: 100%;
			box-shadow: 0 0 10px 0 #efefef;
			background: #fff;
			padding: 30rpx;

			.form-title {
				font-size: 36rpx;
				font-weight: bold;
			}

			.sub-title {
				font-size: 26rpx;
				color: #999;
			}

			.form-item {
				display: flex;
				flex-direction: column;
				border-bottom: 1px solid #efefef;
				padding-top: 30rpx;

				.form-label {
					font-size: 28rpx;
					color: #999;
				}

				.form-content {
					display: flex;
					flex-direction: row;
					align-items: center;

					.form-ctrl {
						height: 100rpx;
						line-height: 100rpx;
						flex-grow: 1;
					}

					.uni-combox__input {
						height: 100rpx;
						line-height: 100rpx;
						flex-grow: 1;
					}

					.arrow {
						flex-grow: 0;
						color: #999;
						font-size: 24rpx;
					}
				}
			}

			.comment-block {
				display: flex;
				flex-direction: column;
				margin-top: 10rpx;

				.label {
					color: #999;
				}

				.textarea {
					font-size: 30rpx;
					padding-top: 30rpx;
					border-bottom: 1px solid #efefef;
				}
			}
		}
	}
</style>
